<template>
    <div>
        <div class="filter-container">
            <el-alert
                description="实际推广费 = 推广费 / 返点"
                type="info"
                :closable="false"
                show-icon>
                <template slot="title">
                    <p>按照约定, 广告计划名称需要以渠道号开头, 比如 '2001-xxxxx', 程序会自动识别渠道号, 同步推广费</p>
                    <p>"自定 - 公式计算"的推广费, 不会体现在当前模块的数据报表中</p>
                </template>
            </el-alert>

            <el-form label-position="left" :inline="true" class="mt-10">
                <el-form-item label="应用渠道">
                    <select-app-source v-model="search.source"></select-app-source>
                </el-form-item>
                <el-form-item label="推广渠道">
                    <el-select v-model="search.channel" clearable class="filter-item" placeholder="推广渠道" style="width: 160px;">
                        <el-option
                            v-for="(item, index) in options.channels"
                            :key="index"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
<!--                        <el-form-item label="广告主ID">-->
<!--                            <el-input class="filter-item" clearable v-model="search.advertiser_id" placeholder="请输入广告主ID"/>-->
<!--                        </el-form-item>-->
                <el-form-item label="">
                    <el-button class="filter-item" icon="el-icon-search" type="primary" :loading="loading" @click="pageNo = 1;handleSearch()">搜索</el-button>
                    <el-button class="filter-item" icon="el-icon-plus" type="primary" @click="create" plain>添加</el-button>
                </el-form-item>
            </el-form>
        </div>

        <el-table
            v-loading="loading"
            ref="multipleTable"
            stripe
            border
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%"
            class="text-center"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="40"/>
            <el-table-column label="应用渠道" prop="source" min-width="130">
                <template slot-scope="scope">
                    <tag-app-source :source="scope.row.source"></tag-app-source>
                </template>
            </el-table-column>
            <el-table-column label="推广渠道" prop="channel" min-width="140">
                <template slot-scope="scope">
                    <el-tag>{{ options.channels[scope.row.channel].label }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="广告主ID" prop="advertiser_id" min-width="130"/>
            <el-table-column label="广告主" prop="advertiser_name" min-width="130"/>
            <el-table-column label="返点" prop="rebate" width="140">
                <template slot-scope="scope">{{ scope.row.rebate/10000 }}</template>
            </el-table-column>
            <el-table-column label="推广费公式" prop="formula_text" min-width="150"/>
            <el-table-column label="操 作" width="150px">
                <template slot-scope="scope">
                    <el-button
                        type="primary"
                        @click="edit(scope.row)">
                        编辑
                    </el-button>
                    <destroy-confirm
                        route="appSourceCpa/Destroy"
                        :primary="scope.row.id"
                        @success="onDestroy">
                    </destroy-confirm>
                </template>
            </el-table-column>
        </el-table>

        <div class="mt-10 pull-left">
            <destroy-multiple
                route="appSourceCpa/DestroyMultiple"
                :selected="selected"
                @success="onDestroy">
                <el-button type="danger"  plain>删除选中</el-button>
            </destroy-multiple>
        </div>

        <el-pagination
            class="pull-right"
            background
            :current-page.sync="pageNo"
            :page-sizes="pageOptions.pageSizesList"
            :page-size="pageSize"
            :layout="pageOptions.layout"
            :total="totalDataNumber"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange">
        </el-pagination>

        <el-dialog
            :title="form.data && form.data.id ? '编辑' : '添加'"
            :visible.sync="form.show"
            width="30%"
            :append-to-body="true"
            :close-on-click-modal="false"
            center>
            <dialog-form
                :data="form.data"
                v-on:submit="onFormSubmit"
                v-on:cancel="form.show = false">
            </dialog-form>
        </el-dialog>

    </div>
</template>

<script>
import dialogForm from './form'

export default {
    name: 'index',
    components: {
        dialogForm,
    },
    data() {
        return {
            search: {
                source: '',
                channel: '',
                // advertiser_id: '',
            },
            loading: false,
            selected: [],
            tableData: [],
            pageNo: 1,
            pageSize: 20,
            pageOptions: this.$store.state.backend.pageOptions,
            totalDataNumber: 0,
            options: this.$store.state.appSourceCpa.options,
            form: {
                data: {},
                show: false,
            }
        }
    },
    methods: {
        handleSizeChange(val) {
            this.pageNo = 1;
            this.pageSize = parseInt(`${val}`);
            this.handleSearch();
        },
        handleCurrentChange(val) {
            this.pageNo = parseInt(`${val}`);
            this.handleSearch();
        },
        handleSelectionChange(val) {
            this.selected = val;
        },
        handleSearch() {
            this.loading = true;
            let filter = Object.assign({}, this.search, {
                page: this.pageNo,
                pageSize: this.pageSize,
            });
            this.$store.dispatch('appSourceCpa/Lists', filter).then(res => {
                this.tableData = res.data.data;
                this.totalDataNumber = res.data.meta.total;
            }).finally(() => {
                this.loading = false;
            });
        },
        initData() {
            this.handleSearch();
        },
        create() {
            this.form.data = {};
            this.form.show = true;
        },
        edit(row) {
            this.form.data = row;
            this.form.show = true;
        },
        onFormSubmit() {
            this.form.show = false;
            this.handleSearch();
        },
        onDestroy() {
            this.handleSearch();
        }
    },
    created() {
        this.initData();
    }
}
</script>

<style lang="scss" scoped>
#app .el-form-item {
    margin-bottom: 0;
}
</style>
